package com.tusuo.stds.ui.level;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.SceneDetail;
import com.tusuo.stds.ui.question.QuestionActivity_;
import com.tusuo.stds.util.Utils;

import org.parceler.Parcels;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

import static com.tusuo.stds.Constants.ACTIVITY_REQUEST_GENERAL;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(LevelPresenter.class)
class LevelPresenter extends MvpBasePresenter<LevelView> {

    @Inject
    ApiHelper mApiHelper;
    @Inject
    SharedPreferences mSharedPref;

    private Activity mActivity;
    private String mErrMsgServer;

    private Subscriber<LevelDetail> mGetLevelDetailSubscriber = null;

    private int mLevelId;
    private LevelDetail mLevelDetail;

    LevelPresenter(Activity activity, int levelId) {
        mActivity = activity;
        mErrMsgServer = mActivity.getResources().getString(R.string.err_msg_server);
        mLevelId = levelId;

        Injector.initLevelPresenterComponent().inject(this);
    }

    @Override
    public void attachView(LevelView view) {
        super.attachView(view);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mGetLevelDetailSubscriber != null) {
            mGetLevelDetailSubscriber.unsubscribe();
            mGetLevelDetailSubscriber = null;
        }
        super.detachView(retainInstance);
    }

    void loadData() {
        mGetLevelDetailSubscriber = new GetLevelDetailSubscriber();
        mApiHelper.getLevelDetail(mGetLevelDetailSubscriber, mLevelId);
    }

    void toQuestionPage(int mLevelId) {
        boolean firstAnswer = mSharedPref.getBoolean("firstAnswer", true);

        Intent intent = new Intent();
        intent.setClass(mActivity, QuestionActivity_.class);
        intent.putExtra(LevelDetail.EXTRA_NAME, Parcels.wrap(mLevelDetail));
        intent.putExtra("firstAnswer", firstAnswer);
        intent.putExtra("levelId",mLevelId);
        mActivity.startActivityForResult(intent, ACTIVITY_REQUEST_GENERAL);
    }

    void isSceneUnlocked(int sceneId) {
        mApiHelper.getSceneDetail(new GetSceneDetailSubscriber(),sceneId);
    }

    private class GetSceneDetailSubscriber extends Subscriber<SceneDetail> {


        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(SceneDetail sceneDetail) {
            int lockStatus = sceneDetail.LockStatus;
            if (lockStatus == 0) {
                getView().jumpToSceneDetail();
            }else if (lockStatus == 10) {
                getView().jumpToSceneList();
            }
        }
    }

    private class GetLevelDetailSubscriber extends Subscriber<LevelDetail> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final LevelDetail levelDetail) {
            if (levelDetail.checkState(mActivity) && getView() != null) {
                getView().setLevelDetail(levelDetail);
                mLevelDetail = levelDetail;
            }
        }
    }
}
